#include<stdio.h>
#include<string.h>

//  i am from nanjing
// -->   nanjing from am i

// 解析： -->  gnijnan morf ma i  --> nanjing from am i

void Reserve(char *s,int len)
{
    //逆序算法
    //首尾互换
    for(int i = 0;i < len / 2;i++)
    {
        char temp = s[i];
        s[i] = s[len-1-i];
        s[len-1-i] = temp;
    }
}

void Reserve2(char *s,int len)
{
    int count = 0;
    for(int i = 0; i <= len ; i++)
    {
        if(s[i] == ' ' || s[i] == '\0')
        {
            //一个单词逆序
            Reserve(s+i-count,count);
            count = 0;
        }
        else{
            count++;  //正常字符
        }
    }
}

int main()
{
    char s[] = "i am from nanjing";
    Reserve(s,strlen(s));
    Reserve2(s,strlen(s));
    printf("%s\n",s);
    return 0;
}